package service.impl;

import dao.AttendanceDao;
import entity.Attendance;
import org.apache.ibatis.session.SqlSession;
import service.AttendanceService;
import util.MyBatisUtil;
import util.Page;

import java.sql.SQLException;
import java.util.List;

public class AttendanceServiceImpl implements AttendanceService {
    SqlSession sqlSession = MyBatisUtil.createSqlSession();
    @Override
    public int deleteAttendanceByEmployeeId(int employeeId) throws SQLException {
        int count = sqlSession.getMapper(AttendanceDao.class).deleteAttendanceByEmployeeId(employeeId);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public Attendance getRecordByEmployeeIdAndDate(int employeeId, String date) throws SQLException {
        Attendance attendance = sqlSession.getMapper(AttendanceDao.class).getRecordByEmployeeIdAndDate(employeeId, date);
        MyBatisUtil.closeSqlSession(sqlSession);
        return attendance;
    }

    @Override
    public int addSinginByEmployeeId(int employeeId,String now) throws SQLException {
        int count = sqlSession.getMapper(AttendanceDao.class).addSinginByEmployeeId(employeeId,now);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public int updateSingoutByAttendanceId(int id) throws SQLException {
        int count = sqlSession.getMapper(AttendanceDao.class).updateSingoutByAttendanceId(id);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }

    @Override
    public List<Attendance> getAttendancesByPage(String name, String searchDate, int pageNo, int pageSize) throws SQLException {
        List<Attendance> list = sqlSession.getMapper(AttendanceDao.class).getAttendancesByPage(name,searchDate,pageNo,pageSize);
        MyBatisUtil.closeSqlSession(sqlSession);
        return list;
    }

    @Override
    public int getAttendancesCount(String name, String searchDate) throws SQLException {
        int count = sqlSession.getMapper(AttendanceDao.class).getAttendancesCount(name,searchDate);
        MyBatisUtil.closeSqlSession(sqlSession);
        return count;
    }
}
